package com.fancon.android.gardenjokesx.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.entity.FacebookUser;
import com.fancon.android.gardenjokesx.entity.TwitterUser;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.social.DatabaseHelper;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.gardenjokesx.utils.UserUtil;

public class S04Activity extends GardenJokesActivity {
	private Button btnSearchFormContact, btnSearchFromFacebook, btnSearchFromTwitter,
			btnSearchFromUserRegister, btnNext, btnSugges;
	private int ScreenID = 0;
	// DatabaseUntil du;
	private FacebookUser currentFacebookUser;
	private TwitterUser currentTwitterUser;
	private DatabaseHelper db;
	private String user_id = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mesx_find_friend_s04);
		// @binhbt
		TextView title = (TextView) findViewById(R.id.action_bar_textview_title);
		title.setText(getText(R.string.s04_title));

		View backBtn = findViewById(R.id.action_bar_button_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		//
		// @binhbt-comment
		// btnNext = (Button) findViewById(R.id.s04_btn_next);
		btnSugges = (Button) findViewById(R.id.s04_btnSugges);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			ScreenID = extras.getInt(Constant.SCREEN_ID);
			if (ScreenID == Constant.U02) {
				btnNext.setVisibility(View.GONE);
				btnSugges.setVisibility(View.VISIBLE);
			}
		}
		User user = UserUtil.getInfoUserLogin(getApplicationContext());
		if (user != null) {
			user_id = user.getUser_id();
		}
		// du = new DatabaseUntil(S04Activity.this);
		db = new DatabaseHelper(S04Activity.this);
		currentFacebookUser = db.getFacebookUser(user_id);
		currentTwitterUser = db.getTwitterUser(user_id);

		btnSearchFormContact = (Button) findViewById(R.id.s04_btn_find_from_contact);
		btnSearchFromFacebook = (Button) findViewById(R.id.s04_btn_find_from_facebook);
		btnSearchFromTwitter = (Button) findViewById(R.id.s04_btn_find_from_twitter);
		btnSearchFromUserRegister = (Button) findViewById(R.id.s04_btn_find_from_user);
		onClick(btnSearchFormContact);
		onClick(btnSearchFromFacebook);
		onClick(btnSearchFromTwitter);
		onClick(btnSearchFromUserRegister);
		// onClick(btnNext);
		onClick(btnSugges);
		// db.close();
	}

	private void onClick(View v) {
		v.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.s04_btn_find_from_contact:
					new AlertDialog.Builder(S04Activity.this)
					.setMessage(R.string.contact_request_confirm)
					.setPositiveButton(R.string.allow,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(
										DialogInterface dialogInf,
										int which) {
									Intent intentS043Activity = new Intent(
											getApplicationContext(), S043Activity.class);
									startActivity(intentS043Activity);
								}

							})
					.setNegativeButton(R.string.common_calcel, null)
					.show();
					break;
				case R.id.s04_btn_find_from_facebook:
					if (currentFacebookUser != null) {
						Intent intentS041Activity = new Intent(
								getApplicationContext(), S041Activity.class);
						intentS041Activity
								.putExtra(Constant.ID_BUTTON, "btnFB");
						startActivity(intentS041Activity);
					} else {
						Intent k = new Intent(getApplicationContext(),
								S03_2Activity.class);
						k.putExtra(Constant.SCREEN_ID, Constant.S04);
						k.putExtra(Constant.ID_BUTTON, "btnFB");
						startActivity(k);
					}
					break;
				case R.id.s04_btn_find_from_twitter:
					if (currentTwitterUser != null) {

						Intent intentS041Activity = new Intent(
								getApplicationContext(), S041Activity.class);
						intentS041Activity.putExtra(Constant.ID_BUTTON,
								"btnTwit");
						startActivity(intentS041Activity);
					} else {
						//@binhbt-comment
						Intent k = new Intent(getApplicationContext(),
								S03_1Activity.class);
						k.putExtra(Constant.SCREEN_ID, Constant.S04);
						k.putExtra(Constant.ID_BUTTON, "btnTwit");
						startActivity(k);
					}
					break;
				case R.id.s04_btn_find_from_user:
					Intent intentS042Activity = new Intent(
							getApplicationContext(), S042Activity.class);
					startActivity(intentS042Activity);
					break;
				case R.id.s04_btnSugges:
					Intent intent = new Intent();
					intent.setClass(getApplicationContext(), S05Activity.class);
					startActivity(intent);
					break;
				default:
					break;
				}
			}
		});

	}

}
